package com.thinkfit.jy.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * 自定义View方式三:重新绘制,继承View
 * 第一步:画出外圆drawArc
 * 第二步:画出进度圆drawArc
 * 第三步:画出文本:中间文本,顶部文本,底部文本drawText
 * Created by cjy on 17/6/14.
 */
public class ArcCircleView extends View {
    private Context mContext;
    /**
     * 文本画笔
     */
    private Paint mTextPaint;
    /**
     * 圆弧画笔
     */
    private Paint mArcCirclePaint;
    /**
     * 宽度
     */
    private float mWidth = 100.0f;
    /**
     * 文本
     */
    private String mText  ="0%";
    /**
     * 底部文本
     */
    private String mTopText  ="";
    /**
     * 底部文本
     */
    private String mBottomText  ="";
    /**
     * 弧度
     */
    private int mAngleValue = 0;
    /**
     * 圆的背景色:默认浅绿色
     */
    private int mCircleBackgroundColor = 0x4c11af9c;
    /**
     * 进度的颜色,默认白色
     */
    private int mProgressColor = 0xffffffff;
    /**
     * 顶部文本的颜色,默认白色
     */
    private int mTopTextColor = 0xffffffff;
    /**
     * 底部文本的颜色,默认白色
     */
    private int mBottomTextColor = 0xffffffff;
    /**
     * 文本的颜色,默认白色
     */
    private int mTextColor = 0xffffffff;
    /**
     * 边宽
     */
    private int mStrokeWidth = 8;
    /**
     * 进度圆边宽
     */
    private int mInnerStrokeWidth = 7;
    /**
     * 文本大小
     */
    private int mTextSize = 12;
    /**
     * 顶部文本大小
     */
    private int mTopTextSize = 10;

    public ArcCircleView(Context context) {
        super(context);
        init(context);
    }

    public ArcCircleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ArcCircleView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context){
        mContext = context;

        mTextPaint  = new Paint();
        //设置抗锯齿
        mTextPaint.setAntiAlias(true);
        //使文本看起来更清晰
        mTextPaint.setLinearText(true);

        mArcCirclePaint  = new Paint();
        mArcCirclePaint.setAntiAlias(true);
        mArcCirclePaint.setStyle(Paint.Style.STROKE);

    }

    public void setWidth(float width){
        mWidth = width;
        invalidate();
    }

    public void setText(String text){
        mText = text;
        invalidate();
    }

    public void setTopText(String text){
        mTopText = text;
        invalidate();
    }

    public void setBottomText(String text){
        mBottomText = text;
        invalidate();
    }

    public void setTextColor(int textColor) {
        this.mTextColor = mContext.getResources().getColor(textColor);
        invalidate();
    }

    public void setBottomTextColor(int bottomTextColor) {
        this.mBottomTextColor = mContext.getResources().getColor(bottomTextColor);
        invalidate();
    }

    public void setTopTextColor(int topTextColor) {
        this.mTopTextColor = mContext.getResources().getColor(topTextColor);
        invalidate();
    }

    public void setProgressColor(int progressColor) {
        this.mProgressColor = mContext.getResources().getColor(progressColor);
        invalidate();
    }

    public void setCircleBackgroundColor(int circleBackgroundColor) {
        this.mCircleBackgroundColor = mContext.getResources().getColor(circleBackgroundColor);
        invalidate();
    }

    public void setStrokeWidth(int strokeWidth){
        this.mStrokeWidth = strokeWidth;
        invalidate();
    }

    public void setInnerStrokeWidth(int innerStrokeWidth){
        this.mInnerStrokeWidth = innerStrokeWidth;
        invalidate();
    }

    public void setTextSize(int textSize){
        this.mTextSize = textSize;
        invalidate();
    }

    public void setTopTextSize(int topTextSize){
        this.mTopTextSize = topTextSize;
        invalidate();
    }

    /**
     * 设置进度
     * @param progress
     */
    public void setProgress(float progress){
        int angleValue = (int) ((progress * 1.0)/100 * 360);
        if (angleValue != 0 && progress <= 100){
            mAngleValue  = angleValue;
            mText = String.valueOf(progress)+"%";
        }
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //1-圆弧的位置:整圆,再绘制进度圆弧
        mArcCirclePaint.setColor(mCircleBackgroundColor);
        mArcCirclePaint.setStrokeWidth(mStrokeWidth);
        //屏幕宽度
        int width = getMeasuredWidth();
        RectF rectF = new RectF();
        rectF.left = (width-mWidth)/2;//左上角X
        rectF.top = mWidth*0.1f;//左上角Y
        rectF.right = (width-mWidth)/2+mWidth;//右上角X
        rectF.bottom = mWidth*0.9f;//右上角Y
        if ((rectF.right - rectF.left) > (rectF.bottom- rectF.top)){//正方形矩形,保证画出的圆不会变成椭圆
            float space = (rectF.right - rectF.left) - (rectF.bottom- rectF.top);
            rectF.left += space/2;
            rectF.right -= space/2;
        }
        canvas.drawArc(rectF,180,360,false,mArcCirclePaint);//第2个参数:时钟12点处为0度,逆时针为正方向

        mArcCirclePaint.setColor(mProgressColor);
        //设置边角为圆
        mArcCirclePaint.setStrokeCap(Paint.Cap.ROUND);
        mArcCirclePaint.setStrokeWidth(mInnerStrokeWidth);
        canvas.drawArc(rectF,180,mAngleValue,false,mArcCirclePaint);

//        //2-文本的位置:居中显示
//        int centerX = width/2;
//        //计算文本宽度
//        int textWidth = (int) mTextPaint.measureText(mText, 0, mText.length());
//        //计算baseline:垂直方向居中
//        Paint.FontMetricsInt fontMetrics = mTextPaint.getFontMetricsInt();
//        int baseline = (getMeasuredHeight() - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;
//        int textX = centerX-textWidth/2;
//        mTextPaint.setColor(mTextColor);
//        mTextPaint.setTextSize(mTextSize);
//        canvas.drawText(mText,textX,baseline,mTextPaint);
//
//        if (mTopText != null && !mTopText.equals("")) {
//            textWidth = (int) mTextPaint.measureText(mTopText, 0, mTopText.length());
//            textX = centerX - textWidth / 2;
//            mTextPaint.setTextSize(mTopTextSize);
//            mTextPaint.setColor(mTopTextColor);
//            canvas.drawText(mTopText, textX, baseline - 20, mTextPaint);
//        }
//
//        if (mBottomText != null && !mBottomText.equals("")) {
//            textWidth = (int) mTextPaint.measureText(mBottomText, 0, mBottomText.length());
//            textX = centerX - textWidth / 2;
////            mTextPaint.reset();
////            mTextPaint.setAntiAlias(true);
////            mTextPaint.setLinearText(true);
//            mTextPaint.setTextSize(mTopTextSize);
//            mTextPaint.setColor(mBottomTextColor);
//            canvas.drawText(mBottomText, textX, baseline + 20, mTextPaint);
//        }
    }

}
